package taro.todolist.serviceimpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import taro.todolist.dao.Member;
import taro.todolist.jparepo.MemberRepository;
import taro.todolist.service.MemberService;

import java.util.List;

@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberRepository memberRepository;


    @Override
    public Member addNewMember(Member member) {
        return memberRepository.save(member);
    }

    @Override
    public void purgeMemberById(Long id) {
        memberRepository.purgeById(id);
    }

    @Override
    public void deleteMemberById(Long id) {
        memberRepository.deleteById(id);
    }

    @Override
    public void recoverMemberById(Long id) {
        memberRepository.recoverById(id);
    }

    @Override
    public Member updateMemberById(Long OldId, Member NewMemberInfo) {
        Member memberInDb = memberRepository.findById(OldId)
                .orElseThrow(() -> new IllegalArgumentException(
                        "[ERROR]请联系开发人员VincentZyu。 [ERROR_INFO]后端web服务器调用服务接口 updateMemberById()时 传入了一个数据库中不存在的id")
                );

        memberInDb.setGame_name(NewMemberInfo.getGame_name());
        memberInDb.setAvatar_path(NewMemberInfo.getAvatar_path());

        return memberRepository.save(memberInDb);
    }

    @Override
    public Member getMemberById(Long id) {
        return memberRepository.findById(id)
                .orElseThrow(() -> new IllegalArgumentException(
                        "[ERROR]请联系开发人员VincentZyu。[ERROR_INFO]后端web服务器调用服务接口 gerMemberById()时 传入了一个数据库中不存在的id")
                );
    }

    @Override
    public String getMemberAvatarPathById(Long id) {
        return memberRepository.getAvatarPathById(id);
    }

    @Override
    public List<Member> getAllMembers() {
        return memberRepository.findAll();
    }

    @Override
    public boolean existById(Long id) {
        return memberRepository.getExistedNumberById(id) > 0;
    }
}
